package 华为OD机试真题2023.进阶题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class HJ15_最少数量线段覆盖 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/进阶题/input/15.txt"));
        MyUtil.START();
        Solution(in);
        MyUtil.END();
    }

    static void Solution(Scanner in) {
        int N = in.nextInt();
        in.nextLine(); // 吸收回车
        List<int[]> list = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            String[] sArray = in.nextLine().split(",");
            int left = Integer.parseInt(sArray[0]);
            int right = Integer.parseInt(sArray[1]);
            list.add(new int[] {left, right});
        }
        list.sort(
                (a, b) -> {
                    return a[0] != b[0] ? a[0] - b[0] : b[1] - a[1];
                });
        int r = list.get(0)[1];
        int maxR = r;
        int res = 1;
        for (int i = 1; i < list.size(); i++) {
            int[] now = list.get(i);
            if (now[0] > r) {
                if (maxR > r) {
                    res++;
                    r = maxR;
                }
                if (now[0] > maxR) {
                    res++;
                    r = now[1];
                }
            }
            maxR = Math.max(maxR, now[1]);
        }
        if (maxR > r) {
            res++;
            r = maxR;
        }
        System.out.println(res);
    }
}
